package com.usc.lw;

import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import com.lw.cinema.mapper.TypeMapper;
import com.yc.cinema.mapper.MovieMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class AppTest2 {
    private SqlSession sqlSession;
    @Before
    public void before() throws IOException {
        String resource = "mybatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 构建会话工厂
        MybatisSqlSessionFactoryBuilder builder = new MybatisSqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(inputStream);
        // 打开会话
        sqlSession = sqlSessionFactory.openSession(true);
    }
    @After
    public void after(){
        System.out.println("after");
        sqlSession.close();
    }

    @Test
    public void test1(){
        MovieMapper movieMapper = sqlSession.getMapper(MovieMapper.class);
        System.out.println(movieMapper.selectById(1));
    }

    @Test
    public void test2(){
        TypeMapper typeMapper = sqlSession.getMapper(TypeMapper.class);
        typeMapper.selectList(null).forEach(System.out::println);
    }
}
